<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="用户姓名:">
        <el-input v-model="queryParams.personName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户编号:">
        <el-input v-model="queryParams.userId" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

     <el-form-item label="工种:">

     <el-select v-model="queryParams.userType" style="width: 100%" filterable clearable placeholder="请选择用户类型" >
       <el-option v-for="option in options.userTypes" :key="option.key" :label="option.lable" :value="option.key" />
     </el-select>
       </el-form-item>
      <el-form-item label="身份证:">
        <el-input v-model="queryParams.idCard" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:hrEmployeeManage:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:hrEmployeeManage:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:hrEmployeeManage:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:hrEmployeeManage:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" :cell-style="cellStyle" highlight-current-row @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="ID" align="center" prop="id" />

      <el-table-column label="姓名" align="center" prop="personName" />
      <el-table-column label="系统ID" align="center" prop="userId" />
      <el-table-column label="用户类型" align="center" prop="userType" />

      <el-table-column label="入职时间" align="center" prop="enrollDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="入职时间" align="center" prop="enrollDate" width="180"/>


      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人员名称" align="center" prop="personName" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.personName }}
        </template>
      </el-table-column>

      <el-table-column label="当前工作强度" align="center" prop="currentJobLoad" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="生日" align="center" prop="dateOfBirth" width="180">

        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateOfBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工资" align="center" prop="salary" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:hrEmployeeManage:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['basic:hrEmployeeManage:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />
    <employee-manage v-if="rowId && detailVisible" :id="rowId" class="detail-view" @close="handleDetailClose" @callback="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="hrEmployeeManageFormDialog.title" :visible.sync="hrEmployeeManageFormDialog.visible" :close-on-click-modal="false" :before-close="resetHrEmployeeManageFormDialog" width="500px" append-to-body>
      <el-form ref="hrEmployeeManageForm" :model="hrEmployeeManageFormData" :rules="hrEmployeeManageFormRules" label-width="80px">
        <el-form-item label="" prop="personName">
          <el-input v-model="hrEmployeeManageFormData.personName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="userId">
          <el-input v-model="hrEmployeeManageFormData.userId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="enrollDate">
          <el-date-picker v-model="hrEmployeeManageFormData.enrollDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择" clearable />
        </el-form-item>
        <el-form-item label="" prop="currentJobLoad">
          <el-input v-model="hrEmployeeManageFormData.currentJobLoad" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="idCard">
          <el-input v-model="hrEmployeeManageFormData.idCard" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="dateOfBirth">
          <el-date-picker v-model="hrEmployeeManageFormData.dateOfBirth" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择" clearable />
        </el-form-item>
        <el-form-item label="" prop="salary">
          <el-input v-model="hrEmployeeManageFormData.salary" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmHrEmployeeManageFormDialog">确 定</el-button>
        <el-button @click="resetHrEmployeeManageFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import HrEmployeeManageApi from '@/api/basic/HrEmployeeManageApi'

import appConstant from '@/constant/AppConstant'

import HrEmployeeManageView from './HrEmployeeManageView.vue'

const defaultHrEmployeeManageFormData = {
  personName: null,  userId: null,  userType: null,  enrollDate: null,  currentJobLoad: null,  idCard: null,  dateOfBirth: null,  salary: null,  status: '0'}
export default {
  name: 'HrEmployeeManageList',
  components: {
    EmployeeManage:HrEmployeeManageView
  },
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions,
           userTypes: appConstant.userType
      },

      queryParams: {
        personName: null,
        userId: null,
        userType: null,
        enrollDate: null,
        currentJobLoad: null,
        idCard: null,
        dateOfBirth: null,
        salary: null,
        status: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      hrEmployeeManageFormDialog: {
        title: '',
        visible: false
      },
      hrEmployeeManageFormData: Object.assign({}, defaultHrEmployeeManageFormData),
      hrEmployeeManageFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true,
      detailVisible: false,
      rowId: null
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openHrEmployeeManageFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return HrEmployeeManageApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openHrEmployeeManageFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      HrEmployeeManageApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return HrEmployeeManageApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openHrEmployeeManageFormDialog(id) {
      this.hrEmployeeManageFormDialog.visible = true
      if (id) {
        this.hrEmployeeManageFormDialog.title = '雇员管理-修改'
        HrEmployeeManageApi.get(id).then(response => {
          this.hrEmployeeManageFormData = {
            ...this.hrEmployeeManageFormData,
            ...response.data
          }
        })
      } else {
        this.hrEmployeeManageFormDialog.title = '雇员管理-添加'
      }
    },
    confirmHrEmployeeManageFormDialog() {
      this.$refs['hrEmployeeManageForm'].validate(valid => {
        if (valid) {
          if (this.hrEmployeeManageFormData.id) {
            HrEmployeeManageApi.edit(this.hrEmployeeManageFormData).then(response => {
              this.$message.success('修改成功')
              this.resetHrEmployeeManageFormDialog()
              this.handleQuery()
            })
          } else {
            HrEmployeeManageApi.add(this.hrEmployeeManageFormData).then(response => {
              this.$message.success('添加成功')
              this.resetHrEmployeeManageFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetHrEmployeeManageFormDialog() {
      this.hrEmployeeManageFormDialog = {
        title: '',
        visible: false
      }
      this.hrEmployeeManageFormData = Object.assign({}, defaultHrEmployeeManageFormData)
      this.resetForm('hrEmployeeManageForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleRowClick(row, column, event) {
      if (column.property === 'personName') {
        this.rowId = row.id
        this.detailVisible = true
      } else {
        this.detailVisible = false
      }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'personName') {
        return {
          color: '#3E84E9',
          cursor: 'pointer'
        }
      } else {
        return ''
      }
    },
    handleDetailClose() {
      this.detailVisible = false
      this.handleQuery()
    }
  }
}
</script>
